<script setup>
import {ref,reactive,onMounted} from "vue"
import myAxios from "../utils/axios";
import {schedulePiniaStore} from "../store/scheduleStore"

let schedulePinia = schedulePiniaStore();
let scheduleList = reactive([])

let loadData = async() =>{
  let {data} = await myAxios.get("/schedule/list");
    // scheduleList = data.data
    // console.log(scheduleList);
    schedulePinia.scheduleList = data.data;
    
    }

//钩子函数：可以自己一来就调用把内容查出来
onMounted(()=>{
    loadData();
})

//保存添加修改的响应式对象
let scheduleInfo = reactive({
    sid:null,
    uid:2,
    title:"学习学习",
    completed:0
})


let saveOrUpdate=async()=>{
  let {data} =await myAxios.post("/schedule/saveOrUpdate",scheduleInfo);
    if(data.code==200){
        alert("操作成功！");
        loadData();
    }
}

let remove = async(sid) =>{
    if(confirm("是否删除")){
        let {data} = await myAxios.delete(`/schedule/remove/${sid}`)
            if(data.code == 200){
                alert("删除成功")
                loadData();
            }
    }
}



</script>
<template>
  <div>
    <h3 class="ht">您的日程如下</h3>
<table class="tab" cellspacing="0px">
    <tr class="ltr">
        <th>编号</th>
        <th>内容</th>
        <th>进度</th>
        <th>操作</th>
    </tr>
    <tr class="ltr" v-for="item,index in schedulePinia.scheduleList" :key="item.sid">
        <td >{{ index+1 }} </td>
        <td>{{ item.title }}</td>
        <td>{{ item.completed == 0?"还不勾八赶快做":"做完了就行" }}</td>
        <td class="buttonContainer">
             <button class="btn1" @click="remove(item.sid)">删除</button>
            <button class="btn1"  @click="saveOrUpdate()">保存修改</button>
        </td>
    </tr>
    <tr class="ltr buttonContainer" >
        <td colspan="4">
            <button class="btn1" @click="saveOrUpdate()">新增日程</button>
        </td>
    </tr>
</table>
  </div>
</template>
<style scoped>
      .ht{
            text-align: center;
            color: cadetblue;
            font-family: 幼圆;
        }
        .tab{
            width: 80%;
            border: 5px solid cadetblue;
            margin: 0px auto;
            border-radius: 5px;
            font-family: 幼圆;
        }
        .ltr td{
            border: 1px solid  powderblue;

        }
        .ipt{
            border: 0px;
            width: 50%;

        }
        .btn1{
            border: 2px solid powderblue;
            border-radius: 4px;
            width:100px;
            background-color: antiquewhite;

        }
        #usernameMsg , #userPwdMsg {
            color: gold;
        }
        .buttonContainer{
            text-align: center;
        }
</style>